package com.dingreading.cloud.subject.service.impl;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.subject.dto.SubjectLessonIssueDto;
import com.dingreading.cloud.subject.entity.SubjectLessonIssue;
import com.dingreading.cloud.subject.entity.table.SubjectLessonIssueTableDef;
import com.dingreading.cloud.subject.mapper.SubjectLessonIssueMapper;
import com.dingreading.cloud.subject.service.SubjectLessonIssueService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-02-26
 */
@Service
public class SubjectLessonIssueServiceImpl extends BaseServiceImpl<SubjectLessonIssueMapper, SubjectLessonIssue, SubjectLessonIssueTableDef> implements SubjectLessonIssueService {

    @Override
    protected SubjectLessonIssueTableDef getTable() {
        return SubjectLessonIssueTableDef.subjectLessonIssue;
    }

    @Override
    public Page<SubjectLessonIssueDto> pageList(
            PageUtil pageUtil, String storeUid, String staffUid, String keywords, String beginTime, String endTime,
            Integer status, String uid
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(staffUid))
            condition.and(table.addStaffUid.eq(staffUid));

        if (StringUtils.isNotBlank(keywords))
            condition.and(table.lessonName.like(keywords)
                    .or(table.levelName.like(keywords))
                    .or(table.storeName.like(keywords))
                    .or(table.addStaffName.like(keywords))
                    .or(table.replyStaffName.like(keywords))
            );
        if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginTime), QueryFiledUtil.appendMin24(endTime)));
        if (status != null)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.eq(uid));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, SubjectLessonIssueDto.class);
    }

    @Override
    public long todayIssueCnt(String storeUid, String staffUid) {
        String time = DateUtil.getNowDateStr();
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.addStaffUid.eq(staffUid))
                .and(table.addTime.likeLeft(time));

        return getMapper().selectCountByCondition(condition);
    }

    @Override
    public List<SubjectLessonIssue> getUnreadIssueNum(String storeUid, String staffUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.addStaffUid.eq(staffUid))
                .and(table.status.eq(2))
                .and(table.readStatus.eq(0));

        return getMapper().selectListByCondition(condition);
    }

    @Override
    public boolean updateReadStatus(String storeUid, String staffUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.addStaffUid.eq(staffUid))
                .and(table.status.eq(2))
                .and(table.readStatus.eq(0));

        return UpdateChain.of(SubjectLessonIssue.class)
                .set(table.readStatus, 1)
                .where(condition)
                .update();
    }
}
